use crate::hal::{interrupt, peripherals};

pub(crate) fn chip_ints_on(mask: u32) {
    unsafe { crate::hal::xtensa_lx::interrupt::enable_mask(mask) };
}

pub(crate) fn chip_ints_off(mask: u32) {
    crate::hal::xtensa_lx::interrupt::disable_mask(mask);
}

pub(crate) unsafe extern "C" fn phy_common_clock_disable() {
    unsafe {
        crate::common_adapter::phy_disable_clock();
    }
}

pub(crate) unsafe extern "C" fn phy_common_clock_enable() {
    unsafe {
        crate::common_adapter::phy_enable_clock();
    }
}

pub(crate) unsafe extern "C" fn set_intr(
    _cpu_no: i32,
    intr_source: u32,
    intr_num: u32,
    _intr_prio: i32,
) {
    unsafe extern "C" {
        fn intr_matrix_set(cpu_no: u32, model_num: u32, intr_num: u32);
    }
    unsafe {
        // Force to bind WiFi interrupt to CPU0
        intr_matrix_set(0, intr_source, intr_num);
    }
}

/// **************************************************************************
/// Name: esp_set_isr
///
/// Description:
///   Register interrupt function
///
/// Input Parameters:
///   n   - Interrupt ID
///   f   - Interrupt function
///   arg - Function private data
///
/// Returned Value:
///   None
///
/// *************************************************************************
pub unsafe extern "C" fn set_isr(
    n: i32,
    f: *mut crate::sys::c_types::c_void,
    arg: *mut crate::sys::c_types::c_void,
) {
    trace!("set_isr - interrupt {} function {:?} arg {:?}", n, f, arg);
    match n {
        0 => unsafe {
            crate::wifi::ISR_INTERRUPT_1 = (f, arg);
        },
        1 => unsafe {
            crate::wifi::ISR_INTERRUPT_1 = (f, arg);
        },
        _ => panic!("set_isr - unsupported interrupt number {}", n),
    }
    #[cfg(feature = "wifi")]
    {
        unwrap!(interrupt::enable(
            peripherals::Interrupt::WIFI_MAC,
            interrupt::Priority::Priority1,
        ));
    }
}
